package com.guiji.job.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.guiji.common.utils.PageUtils;
import com.guiji.common.core.page.PagePlus;
import com.guiji.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.guiji.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.guiji.job.bo.JobAuditRecordAddBo;
import com.guiji.job.bo.JobAuditRecordQueryBo;
import com.guiji.job.bo.JobAuditRecordEditBo;
import com.guiji.job.domain.JobAuditRecord;
import com.guiji.job.mapper.JobAuditRecordMapper;
import com.guiji.job.vo.JobAuditRecordVo;
import com.guiji.job.service.IJobAuditRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 任务审核记录Service业务层处理
 *
 * @author 钟维澎（四月）
 * @date 2021-07-02
 */
@Service
public class JobAuditRecordServiceImpl extends ServicePlusImpl<JobAuditRecordMapper, JobAuditRecord> implements IJobAuditRecordService {

    @Override
    public JobAuditRecordVo queryById(Long id){
        return getVoById(id, JobAuditRecordVo.class);
    }

    @Override
    public TableDataInfo<JobAuditRecordVo> queryPageList(JobAuditRecordQueryBo bo) {
        PagePlus<JobAuditRecord, JobAuditRecordVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), JobAuditRecordVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<JobAuditRecordVo> queryList(JobAuditRecordQueryBo bo) {
        return listVo(buildQueryWrapper(bo), JobAuditRecordVo.class);
    }

    private LambdaQueryWrapper<JobAuditRecord> buildQueryWrapper(JobAuditRecordQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JobAuditRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, JobAuditRecord::getId, bo.getId());
        lqw.eq(bo.getJobId() != null, JobAuditRecord::getJobId, bo.getJobId());
        lqw.eq(bo.getResult() != null, JobAuditRecord::getResult, bo.getResult());
        lqw.eq(StrUtil.isNotBlank(bo.getOpinion()), JobAuditRecord::getOpinion, bo.getOpinion());
        lqw.eq(bo.getVersion() != null, JobAuditRecord::getVersion, bo.getVersion());
        lqw.eq(bo.getCreateTime() != null, JobAuditRecord::getCreateTime, bo.getCreateTime());
        lqw.eq(StrUtil.isNotBlank(bo.getCreateBy()), JobAuditRecord::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, JobAuditRecord::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StrUtil.isNotBlank(bo.getUpdateBy()), JobAuditRecord::getUpdateBy, bo.getUpdateBy());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(JobAuditRecordAddBo bo) {
        JobAuditRecord add = BeanUtil.toBean(bo, JobAuditRecord.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(JobAuditRecordEditBo bo) {
        JobAuditRecord update = BeanUtil.toBean(bo, JobAuditRecord.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(JobAuditRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
